﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainigWorkshop.BLL;
using TrainigWorkshop.Model;
using System.Text.RegularExpressions;

namespace Presentation
{
    public partial class UpdateStudent : System.Web.UI.Page
        
    {
        Boolean type;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = Convert.ToBoolean(Request["type"]);
                if (!IsPostBack)
                {   
                    if (type==true)
                    {
                       BindClass();
                    }
                    else
                    {
                       BindClass();
                       BindStudentInfo();
                    }
                }
         }

        private void BindClass()
        {
            StudentLogic studentbll = new StudentLogic();
            studentbll.GetAllClassInfo();
            ddlClassName.DataSource = studentbll.GetAllClassInfo();
            ddlClassName.DataValueField = "ClassID";
            ddlClassName.DataTextField = "ClassName";
            ddlClassName.DataBind();
        }

        private void BindStudentInfo()
        {
            int studentId = Convert.ToInt32(Request["ID"]);
            StudentLogic sb = new StudentLogic();
            Grade grade = new Grade();

            //get student
            StudentModel student = sb.GetStudentInfoById(studentId);

            this.txtStudentName.Text = student.StudentName;
            if (student.StudentGender.Trim().ToLower() == "female")
            {
                this.RadioBtnFemale.Checked = true;
            }
            else
            {
                this.RadioBtnMale.Checked = true;
            }
            this.ddlClassName.SelectedValue = student.grades.ClassId.ToString().Trim();
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        { 
                string studentName = txtStudentName.Text.Trim();
                string regPatten = "^[A-Za-z]+$";
                Regex reg = new Regex(regPatten);
                if (studentName == "")
                {
                    Response.Write("<script>alert('Please fill a StudentName')</script>");
                }
                if (studentName.Length > 10)
                {
                    Response.Write("<script>alert('The length of StudentName should be less than 10')</script>");
                }
                if (reg.Match(studentName).Success == false)
                {
                    Response.Write("<script>alert('StudentName should be alphabet')</script>");
                }
                if (studentName != "" && studentName.Length <= 10)
                {
                    if (reg.Match(studentName).Success)
                    {    
                        if (type == true)
                        {
                            InsertStudentInfo();
                        }
                        else
                        {
                        UpdateStudentInfo();
                        }
                        Response.Redirect("StudentMainPage.aspx");
                    }
                }
        }

        private void InsertStudentInfo()
        {
            Grade grade = new Grade();
            StudentModel student = new StudentModel(grade);
            student.StudentName = txtStudentName.Text.Trim();
            student.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
            student.grades.ClassId = int.Parse(ddlClassName.SelectedValue.ToString());
            StudentLogic studentbll = new StudentLogic();
            studentbll.InsertStudentInfo(student);
        }

        private void UpdateStudentInfo()
        {
            Grade grade = new Grade();
            StudentModel stu = new StudentModel(grade);
            stu.StudentName = txtStudentName.Text.Trim();
            stu.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
            stu.grades.ClassId= int.Parse(ddlClassName.SelectedValue);
            stu.StudentId = Convert.ToInt16(Request["ID"]);
            StudentLogic studentbll = new StudentLogic();
            studentbll.UpdateStudentInfo(stu);
        }
    }
}